package com.cm.tianwt.socks;

import java.lang.ref.SoftReference;

import com.cm.tianwt.socks.utils.Configs;
import com.cm.tianwt.socks.utils.LogUtils;

public class App  {
	

	public static void main(String[] args) {
		
		LogUtils.setLevel(LogUtils.LEVEL_DEBUG);
		Configs.IS_DEBUG = true;
		Configs.setNeedAuthUserInfo(true);
		
		final SocksProxyServer server = new SocksProxyServer(8888);
		startServer(server);
		LogUtils.D("SocksServer has stopped to serve ");
	}


	private static void startServer(final SocksProxyServer server) {
		
		try {
			Runtime.getRuntime().addShutdownHook(new ShutDownHook(server));
			server.start();
			server.join();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	
	public static class ShutDownHook extends Thread{
		
		private SoftReference<SocksProxyServer> server;

		public ShutDownHook(SocksProxyServer server) {
			super();
			this.server = new SoftReference<SocksProxyServer>(server);
		}
		
		@Override
		public void run() {
			super.run();
			if(this.server==null) return;
			SocksProxyServer socksProxyServer = this.server.get();
			if(socksProxyServer!=null) {
				socksProxyServer.shutDown();
			}
			LogUtils.E("Java Process is Destroyed !");
			
		}
		
	}


}
